import { ref } from "vue"
import { getMoment,getToDayTime } from "../../../utils/time";
import { orderStatusToggle, tableModuleToggle } from "../../utils/status-toggle";
import { myAccountOverviewStore } from "../../store";
import { storeToRefs } from "pinia";
import { objectAny } from "../../../options/base";
import { onReachBottom, onShow } from "@dcloudio/uni-app";
import { db_riderGetOrderDetails } from "../../db/order-details";
import { goToPage, showModal } from "../../../utils/utils";

export default {
    setup() {
        const toDayDate = getMoment().format(`YYYY-MM-DD`);
        const toDayTimeObject= getToDayTime();

        const accountOverviewStore=myAccountOverviewStore();
         // 用户基本信息包括仪表盘数据
        const {accountOverview } = storeToRefs(accountOverviewStore);
        const loadMoreConfig=ref<{
            text:objectAny
        }>({
            text:{
                contentdown: '上拉加载更多数据',
                contentrefresh: '正在加载，请稍后...',
                contentnomore: '----- 暂无更多订单数据 -----'
            }
        })
        // 头部数据统计
        const headStatistics= ref({
            completeCount:0,        //完成订单数量
            cancelCount:0,          //取消订单数量
            turnCount:0,           //转单订单数量
        })
        const currentSelectDate = ref({
            toDay:true,
            text:toDayDate
        })
        // tab 选中的索引
        const checkedIndex = ref(0);
        // 类型选择菜单列表
        const typeItemList = ref<Array<{
            name:string;
            value:0|1|2;
            checked:boolean;
            pageSize:number;
            status:`more`|`loading`|`noMore`|`-1`;
            startTime:number;
            endTime:number;
            dataList:Array<objectAny>
        }>>([{
            name:`已完成`,
            value:0,
            checked:true,
            pageSize:5,
            status:`-1`,
            dataList:[],
            ...toDayTimeObject,
        },{
            name:`已取消`,
            value:1,
            checked:false,
            pageSize:5,
            status:`-1`,
            dataList:[],
            ...toDayTimeObject,
        },{
            name:`已转单`,
            value:2,
            checked:false,
            pageSize:5,
            status:`-1`,
            dataList:[],
            ...toDayTimeObject,
        }])

        onShow(()=>{
            getMyAccountOverview();
        })
        
        onReachBottom(()=>{
            getMyAccountOverview();
        })

        // 开始获取列表数据
        async function getMyAccountOverview(){
            const tabIndex = checkedIndex.value
            const tabItemData = typeItemList.value[tabIndex];

            if(tabItemData.status==='loading'){
                return console.warn(`======> getMyAccountOverview 正在获取数据，无需多次获取，本次获取取消<======`)
            }
            tabItemData.status='loading';

            let pageSize=tabItemData.pageSize;
            if(tabItemData.dataList.length >= pageSize){
                tabItemData.pageSize+=5;
                pageSize=tabItemData.pageSize
            }
            try {
                const {data,myErrCode,errMsg} =await db_riderGetOrderDetails({
                    pageSize:tabItemData.pageSize,
                    status:tabItemData.value,
                    startTime:tabItemData.startTime,
                    endTime:tabItemData.endTime,
                    justToDay:false,
                    statistics:true
                });
                tabItemData.status='more';
                if(myErrCode !== 0){
                    tabItemData.pageSize-=5;
                    return showModal({
                        title:`异常提示`,
                        content:errMsg,
                        showCancel:false
                    }) 
                }
                const listData = data?.list as Array<objectAny>;
                if(pageSize > listData.length){ //无更多数据
                    tabItemData.status='noMore';
                }
                const sortList = listData.sort((a,b)=>a.order_end_time - b.order_end_time)

                tabItemData.dataList=sortList
                if(data?.statistics){
                    headStatistics.value = data?.statistics
                }
            } catch (error) {
                tabItemData.status='more';
                console.error(`xxxxxx=> 获取 getMyAccountOverview 列表错误，错误信息如下： <=xxxxxx`)
                console.error(error)
                return showModal({
                    title:`异常提示`,
                    content:`访问过于火爆，请稍后再试`,
                    showCancel:false
                }) 
            }
        }


        function dateChange(date:string){
            currentSelectDate.value={
                toDay:date==toDayDate,
                text:date
            }
            const {
                startTime,
                endTime
            } = getToDayTime(date)
            const tabList = typeItemList.value
            for(let i=0;i<tabList.length;i++){
                typeItemList.value[i].startTime=startTime;
                typeItemList.value[i].endTime=endTime;
            }
            getMyAccountOverview();
        }

        function typeItemChange(index:number){
            typeItemList.value.map(it=>it.checked=false);
            typeItemList.value[index].checked=true;
            checkedIndex.value=index
            getMyAccountOverview();
        }

        function goToOtherPage(path:string,params:objectAny|undefined={}){
            goToPage({
                path,
                navType: `push`,
                params
            });
        }


        return {
            headStatistics,
            checkedIndex,
            loadMoreConfig,
            accountOverview,
            tableModuleToggle,
            orderStatusToggle,
            
            currentSelectDate,
            typeItemList,
            dateChange,
            typeItemChange,
            getMoment,
            goToOtherPage
        }
    }
}